package cn.edu.nwpu.warehousemanagement.controller;

import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.pojo.request.AdminUpdatePWRequest;
import cn.edu.nwpu.warehousemanagement.pojo.request.AdminUpdateUserInfoRequest;
import cn.edu.nwpu.warehousemanagement.pojo.request.RegisterRequest;
import cn.edu.nwpu.warehousemanagement.service.AdminService;
import cn.edu.nwpu.warehousemanagement.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员接口
 *
 * @author Loadstar
 * @version 1.0
 */
@RestController
@RequestMapping("/api/admin")
//@Validated
public class AdminController {
    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;

    /**
     * @param registerRequest 注册请求
     * @return 注册结果
     */
    @PostMapping("/userRegister")
    public Result<?> register(
            @Valid @RequestBody RegisterRequest registerRequest) {
        return userService.register(registerRequest);
    }

    @PatchMapping("/updateUserPassword")
    public Result<?> updateUserPassword(
            @Valid @RequestBody AdminUpdatePWRequest adminUpdatePWRequest) {
        return userService.adminUpdatePassword(adminUpdatePWRequest);
    }


    @DeleteMapping("/deleteUser/{userId}")
    public Result<?> deleteUser(@PathVariable Long userId) {
        return adminService.deleteUser(userId);
    }

    @GetMapping("/userList")
    public Result<?> userList(@RequestParam(defaultValue = "0") int page,
                              @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return adminService.findAllUser(pageable);
    }

    @PatchMapping("/updateUserInfo")
    public Result<?> updateUserInfo(@RequestBody AdminUpdateUserInfoRequest updateUserInfoRequest) {
        return adminService.updateUserInfo(updateUserInfoRequest);
    }


}
